//
//  reviseViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/5/9.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "reviseViewController.h"
#import "PasswordViewController.h"
#import "AdressViewController.h"
#import "CONST.h"

@interface reviseViewController ()<UITextFieldDelegate>
{
    UILabel *liveadLabel ;
    UIView *addressView ;
    UILabel *addressLabel ;
}
@end

@implementation reviseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    //liveadLabel.text = _addressStr ;
    [self downloadPersonalDate] ;
}

- (void)downloadPersonalDate
{
    NSUserDefaults *uidDefaults = [NSUserDefaults standardUserDefaults] ;
    NSInteger uidInt = [[uidDefaults valueForKey:@"uid"] integerValue] ;
    
    NSNumber *cmd = [[NSNumber alloc] initWithInt:8] ;
    NSNumber *uid = [[NSNumber alloc] initWithInteger:uidInt] ;
    NSDictionary *parameters = @{@"cmd":cmd,@"uid":uid};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:getAppJson parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         //解密并解析json数据，将json转成字典
         NSString *decodeStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] ;
         NSLog(@"decodeStr:%@",decodeStr) ;
         decodeStr = [CodeWithDES decodeWithStr:decodeStr] ;
         NSLog(@"decodeStr1:%@",decodeStr) ;
         NSData *decodeData = [decodeStr dataUsingEncoding:NSUTF8StringEncoding] ;
         id result = [NSJSONSerialization JSONObjectWithData:decodeData options:NSJSONReadingMutableContainers error:nil] ;
         //获取字典中的值
         if ([result isKindOfClass:[NSDictionary class]])
         {
             NSDictionary *dic = result ;
             // NSLog(@"dic:%@",dic) ;
             if (([dic[@"status"] integerValue] == 1) && ![dic[@"content"] isKindOfClass:[NSNull class]])
             {
                 for (NSDictionary *content in dic[@"content"])
                 {
                     [_model setValuesForKeysWithDictionary:content] ;
                     [self createlistView] ;
                 }
             }
             
         }
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error: %@", error);
     }];
}

- (void)createlistView
{
    UIView *passwordView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, 50)] ;
    passwordView.backgroundColor = [UIColor whiteColor] ;
    passwordView.userInteractionEnabled = YES ;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer
                                                     alloc] initWithTarget:self action:@selector(pushPassword)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [passwordView addGestureRecognizer: tapGestureRecognizer];
    
    UILabel *myPasswordLabel = [[UILabel alloc] init] ;
    myPasswordLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    myPasswordLabel.backgroundColor = [UIColor clearColor] ;
    myPasswordLabel.text = @"我的密码" ;
    myPasswordLabel.textAlignment = NSTextAlignmentCenter ;
    myPasswordLabel.textColor = [UIColor grayColor] ;
    CGSize labelLength = [self lengthwithLabel:myPasswordLabel withsize:16.0f] ;
    myPasswordLabel.frame = CGRectMake(10, 17, labelLength.width, 18.0f) ;
    [passwordView addSubview:myPasswordLabel] ;

    //账号最右边图片
    UIImageView *rightImage = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth - 44, 12 , 24, 24)] ;
    [rightImage setImage:[UIImage imageNamed:@"editing_icon.png"]] ;
    
    [passwordView addSubview:rightImage] ;
    [self.view addSubview:passwordView] ;

    
    addressView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, __kScreenWidth, 50)] ;
    addressView.backgroundColor = [UIColor whiteColor] ;
    addressView.userInteractionEnabled = YES ;
    UITapGestureRecognizer *addressTap = [[UITapGestureRecognizer
                                                     alloc] initWithTarget:self action:@selector(pushAddress)];
    addressTap.numberOfTapsRequired = 1;
    [addressView addGestureRecognizer: addressTap];
    
    addressLabel = [[UILabel alloc] init] ;
    addressLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    addressLabel.backgroundColor = [UIColor clearColor] ;
    addressLabel.text = @"修改所在地" ;
    addressLabel.textAlignment = NSTextAlignmentCenter ;
    addressLabel.textColor = [UIColor grayColor] ;
    CGSize addressLength = [self lengthwithLabel:addressLabel withsize:16.0f] ;
    addressLabel.frame = CGRectMake(10, 17, addressLength.width, 18.0f) ;
    [addressView addSubview:addressLabel] ;
    
    liveadLabel = [[UILabel alloc] init] ;
    liveadLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:18.0f] ;
    liveadLabel.backgroundColor = [UIColor clearColor] ;
    liveadLabel.text = _addressStr ;
    liveadLabel.textAlignment = NSTextAlignmentLeft ;
    liveadLabel.textColor = [UIColor blackColor] ;
    CGFloat width = __kScreenWidth - (addressLabel.frame.origin.x + addressLabel.frame.size.width + 104) ;
    liveadLabel.frame = CGRectMake(addressLabel.frame.origin.x + addressLabel.frame.size.width + 30, 16, width, 20.0f) ;
    [addressView addSubview:liveadLabel] ;
    
    //账号最右边图片
    UIImageView *rightImage2 = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth - 44, 12 , 24, 24)] ;
    [rightImage2 setImage:[UIImage imageNamed:@"editing_icon.png"]] ;
    
    [addressView addSubview:rightImage2] ;
    [self.view addSubview:addressView] ;
    
    
    UIView *RZView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, __kScreenWidth, 50)] ;
    RZView.backgroundColor = [UIColor whiteColor] ;
    UILabel *RZLabel = [[UILabel alloc] init] ;
    RZLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    RZLabel.backgroundColor = [UIColor clearColor] ;
    RZLabel.text = @"实名认证" ;
    RZLabel.textAlignment = NSTextAlignmentCenter ;
    RZLabel.textColor = [UIColor grayColor] ;
    CGSize RZLabelLength = [self lengthwithLabel:RZLabel withsize:16.0f] ;
    RZLabel.frame = CGRectMake(10, 17, RZLabelLength.width, 18.0f) ;
    [RZView addSubview:RZLabel] ;
    
    UILabel *rzStrLabel = [[UILabel alloc] init] ;
    rzStrLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:18.0f] ;
    rzStrLabel.backgroundColor = [UIColor clearColor] ;
    rzStrLabel.textAlignment = NSTextAlignmentLeft ;
    
    
    if ([_RzStr integerValue] == 1) {
        rzStrLabel.text = @"已认证" ;
        rzStrLabel.textColor = [UIColor redColor] ;
    }
    else
    {
        rzStrLabel.text = @"未认证" ;
        rzStrLabel.textColor = [UIColor grayColor] ;
    }
    CGSize rzStrLength = [self lengthwithLabel:rzStrLabel withsize:18.0f] ;
    rzStrLabel.frame = CGRectMake((__kScreenWidth - rzStrLength.width)/2, 16, rzStrLength.width, 20.0f) ;
    [RZView addSubview:rzStrLabel] ;
    
    [self.view addSubview:RZView] ;
    
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, __kScreenWidth, 0.5)] ;
    [line setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [self.view addSubview:line] ;
    
    UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, __kScreenWidth, 0.5)] ;
    [line2 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [self.view addSubview:line2] ;
    
    UIImageView *line3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 150, __kScreenWidth, 0.5)] ;
    [line3 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [self.view addSubview:line3] ;
    
    
}

- (void)pushPassword
{
    PasswordViewController *vc = [[PasswordViewController alloc] init] ;
    self.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:vc animated:NO] ;
}

- (void)pushAddress
{
    AdressViewController *vc = [[AdressViewController alloc] init] ;
    vc.model = _model ;
    self.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:vc animated:NO] ;
}

- (void)cleartext
{
}

- (CGSize)lengthwithLabel:(UILabel *)lable withsize:(CGFloat) size
{
    NSDictionary *attribute1 = @{NSFontAttributeName: [UIFont fontWithName:@"AvenirLTStd-Light" size:size]};
    CGSize DescSize = [lable.text boundingRectWithSize:CGSizeMake(MAXFLOAT, size) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    return DescSize ;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO ;
    self.navigationItem.title = @"账户修改" ;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    self.view.backgroundColor = [UIColor whiteColor] ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
